from manim import *



class math(Scene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
        
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])   
        
         #设置题目并写出 
        text = Tex(r"\text{设数列} $x_{n}$"+
                   r"\text{满足：}$0 <x_{n} <\dfrac{\pi }{2},x_{n}\cos x_{n+1}=\sin x_{n},n=1,2,\ldots $"+
                   r"\text{证明}$\left\{ x_{n}\right\} $"+
                   r"\text{收敛，并求} $\lim\limits _{n\rightarrow \infty }x_{n}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        self.play(Write(text),FadeIn(dot1),FadeIn(dot2),FadeIn(dot3))
        #题目向上移动
        self.wait()
        self.play(text.animate().scale(0.8).move_to(UP*20)) 

        #分析文本
        text0=MarkupText("<b><i>分析：</i></b>",color=PINK).scale(5.0).move_to([-35,7.5,0])
        self.play(Create(text0))
        self.wait()
        #中心主题+框框标记
        text1 = Tex(r"\text{证明数列极限收敛}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,0,0])
        self.play(Write(text1))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=23,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-35, 0, 0])
        self.play(Create(rectangle))
        self.wait()
        #箭头引出
        arrow = Arrow(start=[-23,2.5,0],end=[-15,10,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本2
        text2 = Tex(r"\text{夹逼定理}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7.5,10,0])
        self.play(Write(text2))
        self.wait()
        #箭头1引出
        arrow1 = Arrow(start=[-23,-2.5,0],end=[-15,-5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow1))
        #文本3
        text3 = Tex(r"\text{单调有界}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7.5,-5,0])
        self.play(Write(text3))
        self.wait()
        #半透明矩阵扩展
        rectangle1=Rectangle(color=YELLOW,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([5,22,0])
        self.play(
            rectangle1.animate.shift(7*RIGHT).stretch(18.0,dim=0)
        )
        self.wait()
        text8 = Tex(r"\text{假设}$\lim\limits _{n\rightarrow \infty }x_{n}=a $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-10,0])
        self.play(Write(text8))
        self.wait()
        text9 = Tex(r"$\acos a=\sin a $",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary. ctex).scale(5.0).move_to([-35,-15,0])
        self.play(Write(text9))
        self.wait()
        #半透明框框移动
        self.play(rectangle1.animate().shift(16*LEFT).stretch(0.7,dim=0))
        self.wait(0.5)
        text10 = Tex(r"$\acos a=\sin a $",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-15,0])
        self.play(Write(text10))
        self.wait(0.5)
        text12 = Tex(r"$a=0$",color=RED_D
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-20,0])
        self.play(Write(text12))
        ##半透明框框淡出
        self.play(FadeOut(rectangle1))
        #框框快速闪动
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-35, -20, 0])
        self.play(Create(rectangle2))
        self.play(FadeOut(rectangle2))
        self.wait(1)
        #框框标记 
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=14,height=5 
                            ,stroke_color=GREEN_D,stroke_width=20)
        rectangle1.move_to([-7.5, 10, 0])
        self.play(Create(rectangle1))
        self.wait(0.5)
        text13 = Tex(r"$0\leq \left| x_{n+1}-a\right| =\left| f\left( x_{n}\right) -f\left( a\right) \right| =\left| f'\left( \xi \right) \right| \cdot \left| x_{n}-a\right| $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,4,0])
        self.play(Write(text13))
        #部分文本淡出
        self.wait(0.5)
        text14 = Tex(r"$\leq \ldots \leq \left| f'\left( \xi _{n}\right) _{\max }\right| ^{n-1}\cdot \left| x_{1}-a\right| $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-1,0])
        self.play(Write(text14),FadeOut(text12,text10,text9,text8))
        #框框快速闪动
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=16,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([19, -1, 0])
        self.play(Create(rectangle3))
        text15 = Tex(r"$ <1$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17.5,-6,0])
        self.play(Write(text15))
        self.play(FadeOut(rectangle3,text15)) 
        #框框标记

        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=28,height=5 
                            ,stroke_color=GREEN_D,stroke_width=20)
        rectangle4.move_to([25, -1, 0]) 
        self.play(Create(rectangle4))
        self.wait(0.5)
        #箭头+文本
        arrow2 = Arrow(start=[40,-1,0],end=[45,-1,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow2))
        self.wait(0.5)
        #文本
        text16 = Tex(r"\text{0}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([47,-1,0])
        self.play(Write(text16))
        self.wait(0.5)
        #淡出部分对象
        self.play(FadeOut(text16,arrow2,rectangle4,text13,text14))
        self.wait(0.5)
        #框框标记

        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=14,height=5 
                            ,stroke_color=GREEN_D,stroke_width=20)
        rectangle5.move_to([-7.5, -5, 0]) 
        self.play(Create(rectangle5))
        self.wait(0.5)
        #文本
        text17 = Tex(r"\text{先猜后证}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-5,0])
        self.play(Write(text17))
        self.wait(0.5)
        #清空屏幕
        self.play(FadeOut(text0,text1,text2,text3,text17,rectangle,rectangle1,rectangle5,arrow,arrow1))
        self.wait(0.5)
        #解答文本
        text18=MarkupText("<b><i>解：</i></b>",color=PINK).scale(5.0).move_to([-35,7.5,0])
        self.play(Create(text18))
        self.wait(0.5)
        #半透明框框

        rectangle6=Rectangle(color=YELLOW,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-10,22,0])
        self.play(
            rectangle6.animate.shift(6*RIGHT).stretch(11.0,dim=0)
        )
        self.wait(0.5)
        text25 = Tex(r"$a=0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,25,0])
        self.play(Write(text25))
        self.play(FadeOut(text25))
        self.wait(0.5)
        #文本
        text19 = Tex(r"\text{猜：}$\left\{ x_{n}\right\} \downarrow $   \text{且}  $x_{n} >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,10,0])
        self.play(Write(text19))
        self.wait(0.5)
        #框框闪动
        rectangle7=Rectangle(color=BLUE,fill_opacity=0.0,width=9,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle7.move_to([8, 10, 0])
        self.play(Create(rectangle7))
        self.wait(0.5)
        text20 = Tex(r"\text{有下界0}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,2,0])
        self.play(Write(text20))
        self.play(FadeOut(text20,rectangle6),rectangle7.animate().shift(12*LEFT))
        self.wait(0.5)
        #箭头标记
         #箭头引出
        arrow3 = Arrow(start=[-5,6,0],end=[-10,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow3))
        self.wait(0.5)
        #文本2
        text21 = Tex(r"$x_{n+1}-x_{n} <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-5,0])
        self.play(Write(text21))
        self.wait(0.5)

        #箭头1引出
        arrow4 = Arrow(start=[-5,6,0],end=[0,0,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow4))
        #文本
        text22 = Tex(r"$\dfrac{x_{n+1}}{x_{n}} <1$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-5,0])
        self.play(Write(text22))
        self.wait(0.5)

        text26 = Tex(r"$x_{n+1}=\arccos \left( \dfrac{\sin x_{n}}{x_{n}}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,25,0])
        self.play(Write(text26))
        self.play(FadeOut(text26))
        self.wait(0.5)
          #箭头引出
        arrow5 = Arrow(start=[-15,-7,0],end=[-15,-15,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow5))
        text24 = Tex(r"$f$\text{单增}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,-10,0])
        self.play(Write(text24))
        self.wait(0.5)
        #文本2
        text23 = Tex(r"$f\left( x_{n+1}\right) -f\left( x_{n}\right)  <0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-17.5,0])
        self.play(Write(text23))
        self.wait(0.5)
        #清空
        self.play(FadeOut(text24,text23,arrow5,text22,arrow4,text21,arrow3,rectangle7,text19))
        self.wait(0.5)
        #半透明框框
        rectangle8=Rectangle(color=YELLOW,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([5,22,0])
        self.play(
            rectangle8.animate.shift(7*RIGHT).stretch(18.0,dim=0)
        )
        self.wait(0.5)
         # 创建坐标系
        axes = Axes(
            x_range=[-3*PI, 3*PI, PI/2],  # 设置 x 轴范围，从 -3π 到 3π，步长为 π/2
            y_range=[-1.5, 1.5, 0.5],     # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE,"stroke_width": 30},  # 设置坐标轴的颜色
            x_length=40,  # 控制 x 轴长度
            y_length=20 ,   # 控制 y 轴长度
        )
        axes.move_to([25,5,0])
        # 绘制 cos(x) 函数图像，负半轴部分为蓝色
        cos_graph_left = axes.plot(lambda x: np.cos(x), color=BLUE,stroke_width=30, x_range=[-2*PI, 2*PI])
        # 正半轴部分为红色
        cos_graph_right = axes.plot(lambda x: np.cos(x), color=RED,stroke_width=30, x_range=[0, PI/2])

        # 添加坐标系和函数图像到场景中
        #self.add(axes, cos_graph_left, cos_graph_right)

        # 添加标题
        title = Text("cos(x)",color=GREEN, font_size=240).next_to(axes, UP)
        self.add(title)

        # 显示图像
        self.play(Create(axes), Create(cos_graph_left),  Write(title), run_time=2)
        self.play(Create(cos_graph_right))
        self.wait(3)
        #文本
        text24 = Tex(r"$\cos x_{n+1}-\cos x_{n}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text24))
        self.wait(0.5)
        #文本快速闪动
        text25 = Tex(r"$ >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text24,RIGHT,buff=1)
        self.play(Write(text25))
        self.play(FadeOut(text25))
        self.wait(0.5)
        #文本移动+图像淡出
        self.play(text24.animate().shift(20*LEFT),FadeOut(axes,cos_graph_left,title,cos_graph_right))
        self.wait(0.5)
        #文本
        text26 = Tex(r"$=\dfrac{\sin x_{n}}{x_{n}}-\cos x_{n}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text26))
        self.wait(0.5)
        #框框闪动
        rectangle9=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle9.move_to([0, 0, 0])
        self.play(Create(rectangle9))
        self.play(FadeOut(rectangle9))
        self.wait(0.5)
        #清除桌面
        self.play(FadeOut(text26,text24))
        self.wait(0.5)
        #文本
        text27 = Tex(r"\text{证明：}$\dfrac{\sin x}{x}-\cos x >0( 0 <x < \dfrac{\pi }{2}) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text27))
        self.wait(0.5)
        self.play(text27.animate().shift(10*UP))
        self.wait(0.5)
        text28 = Tex(r"$\dfrac{sinx}{x} >\cos x$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text28))
        self.wait(0.5)
        text29 = Tex(r"$\sin x >x\cos x$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        text30 = Tex(r"$\sin x-x\cos x >0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(
            ReplacementTransform(text28,text29)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text29,text30)
        )
        #框框标记
        rectangle10=Rectangle(color=BLUE,fill_opacity=0.0,width=16,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle10.move_to([-2, 0, 0])
        self.play(Create(rectangle10))
        self.wait(0.5)
        #文本
        text31 = Tex(r"$F\left( x\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-5,0])
        self.play(Write(text31))
        self.wait(0.5)
        #箭头标记+文本
        arrow6 = Arrow(start=[5,-5,0],end=[10,-5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow6))
        self.wait(0.5)

        text32 = Tex(r"$F\left( 0\right) =0$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,-5,0])
        self.play(Write(text32))
        self.wait(0.5)
        #箭头标记+文本
        arrow7 = Arrow(start=[21,-5,0],end=[25,-5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow7))
        self.wait(0.5)
        text33 = Tex(r"$F\left( x\right) \uparrow $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-5,0])
        self.play(Write(text33))
        self.wait(0.5)
        #文本
        text34 = Tex(r"$F'\left( x\right) =\sin x >0$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-10,0])
        self.play(Write(text34))
        self.wait(0.5)
         #清除桌面
        self.play(FadeOut(text30,text34,text33,arrow7,text32,arrow6,text31,rectangle10,text28,text27))
        
        #显示一些已有元素
        self.play(Create(text19))
        self.play(Create(rectangle7))
        text35 = Tex(r"\text{单调递减}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,2,0])
        self.play(Write(text35))
        self.play(rectangle7.animate().shift(12*RIGHT))
        self.play(Create(text20))

        



        


















         






        

        
        














       